/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelChat.java
 *
 * Created on 24.06.2011, 9:25:21
 */
package ru.icq.gui.panels;

import org.apache.log4j.Logger;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;
import ru.icq.accounts.EntryjICQ;

/**
 * Панель с чатом. Непосредственное отображение чата с выбранным пользователем.
 * @author Носов А.В.
 */
public class PanelChat extends javax.swing.JPanel implements MessageListener {

    // Variables declaration
    private static final Logger log = Logger.getLogger(PanelChat.class);
    
    private EntryjICQ entry;
    // End of variables declaration

    /** Creates new form PanelChat */
    public PanelChat(EntryjICQ entryjICQ) {
        this.entry = entryjICQ;
        entry.getConnectionCustom().createChat(entryjICQ.getUser(), this);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitPaneChat = new javax.swing.JSplitPane();
        scrollPaneChatHistory = new javax.swing.JScrollPane();
        textPaneChatHistory = new javax.swing.JTextPane();
        scrollPaneMessage = new javax.swing.JScrollPane();
        textPaneMessage = new javax.swing.JTextPane();
        panelSendMsg = new javax.swing.JPanel();
        buttonSendMsg = new javax.swing.JButton();

        setName("chat"); // NOI18N
        addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentRemoved(java.awt.event.ContainerEvent evt) {
                formComponentRemoved(evt);
            }
        });
        setLayout(new java.awt.BorderLayout());

        splitPaneChat.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        scrollPaneChatHistory.setViewportView(textPaneChatHistory);

        splitPaneChat.setLeftComponent(scrollPaneChatHistory);

        scrollPaneMessage.setViewportView(textPaneMessage);

        splitPaneChat.setRightComponent(scrollPaneMessage);

        add(splitPaneChat, java.awt.BorderLayout.CENTER);

        panelSendMsg.setLayout(new java.awt.GridLayout(1, 0));

        buttonSendMsg.setText("Send");
        buttonSendMsg.setName("chat.buttonSend"); // NOI18N
        buttonSendMsg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSendMsgActionPerformed(evt);
            }
        });
        panelSendMsg.add(buttonSendMsg);

        add(panelSendMsg, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSendMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSendMsgActionPerformed
        if (textPaneMessage.getText().trim().length() > 0) {
            textPaneChatHistory.setText(textPaneChatHistory.getText() +
                    "мои сообщения" + ":\n" + 
                    textPaneMessage.getText().trim() + "\n");
            entry.getConnectionCustom().sendMessage(entry.getUser(), 
                    textPaneMessage.getText().trim());
            textPaneMessage.setText("");
        }
    }//GEN-LAST:event_buttonSendMsgActionPerformed

private void formComponentRemoved(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_formComponentRemoved
    //entry.getConnectionCustom().closeChat(entry.getUser());
    log.info("Уничтожить");
}//GEN-LAST:event_formComponentRemoved

    @Override
    public void processMessage(Chat chat, Message msg) {
        if(msg.getType() == Message.Type.chat) {
            textPaneChatHistory.setText(textPaneChatHistory.getText() +
                    msg.getFrom() + ":\n" + msg.getBody() + "\n");
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSendMsg;
    private javax.swing.JPanel panelSendMsg;
    private javax.swing.JScrollPane scrollPaneChatHistory;
    private javax.swing.JScrollPane scrollPaneMessage;
    private javax.swing.JSplitPane splitPaneChat;
    private javax.swing.JTextPane textPaneChatHistory;
    private javax.swing.JTextPane textPaneMessage;
    // End of variables declaration//GEN-END:variables

}
